﻿using KFC.MonAnF;
using KFC.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KFC
{
    public partial class MappingXML2Object : Form
    {

        private List<MapField2Element> maps
            = new List<MapField2Element>();

        public List<MapField2Element> Maps
        {
            get { return maps; }
            set { maps = value; }
        }
        private List<String> _listXMLs;
        private List<String> _listObjects;

        private List<ComboBox> _listComboBoxXMLs = new List<ComboBox>();
        private List<ComboBox> _listComboBoxObjects = new List<ComboBox>();
        public MappingXML2Object(List<String> xmls)
        {
            _listXMLs = xmls;
            _listObjects = MonAnFactory.LayDSThuocTinhMonAn();

            for(int i = 0; i<_listXMLs.Count; i++)
            {
                 ComboBox cbXML = new ComboBox();
                cbXML.SelectedIndexChanged += new EventHandler(cbXML_SelectedIndexChanged);            
                InitComboBox(_listXMLs, _listComboBoxXMLs, i, cbXML,70);
                // khoi tao cac comboBox SQL
                ComboBox cbSQL = new ComboBox();
                cbSQL.SelectedIndexChanged += new EventHandler(cbSQL_SelectedIndexChanged);
                InitComboBox(_listObjects,_listComboBoxObjects, i, cbSQL, 270);
            }

            foreach (ComboBox c in _listComboBoxXMLs)
            {
                this.Controls.Add(c);
            }
            foreach (ComboBox c in _listComboBoxObjects)
            {
                this.Controls.Add(c);
            }        
            InitializeComponent();
        }

        private void InitComboBox(List<string> str,List<ComboBox> list, int i, ComboBox comboBox, int _x)
        {
            
            foreach (string x in str)
            {
                comboBox.Items.Add(x);
            }
            
            int _y = 50 + i * comboBox.Height + 20;
            comboBox.Location = new System.Drawing.Point(_x, _y);
            list.Add(comboBox);
        }

        List<String> listSelectedString = new List<string>();
        void cbXML_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = ((ComboBox)sender).SelectedItem.ToString();
            
            listSelectedString.Add(selected);

            List<string> newListString = new List<string>();
            //newListString = listXMLString;
            for (int i = 0; i < _listXMLs.Count; i++)
            {
                newListString.Add(_listXMLs[i]);
            }

            // xoa cac item da dc chon
            foreach (string strXML in _listXMLs)
            {
                foreach (string s in listSelectedString)
                {
                    if (strXML == s)//neu item do da dc chon
                    {
                        newListString.Remove(strXML);
                    }
                }
            }
            foreach (ComboBox cb in _listComboBoxXMLs)
            {
                if (cb.SelectedItem == null)//chua dc chon
                {
                    cb.Items.Clear();
                    foreach (string s in newListString)// chen item moi
                        cb.Items.Add(s);
                }
            }
        }

        void cbSQL_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void button_Map_Click(object sender, EventArgs e)
        {
            try
            {
                // khoi tao             
                foreach (var v in _listObjects)
                {
                    MapField2Element element = new MapField2Element(){Field = v};
                    maps.Add(element);
                }

                // gan cac comboBoxXML vao cac MapElement
                for (int i = 0; i < this._listComboBoxXMLs.Count; i++)
                {
                    if (_listComboBoxXMLs[i].SelectedItem != null)
                    {
                        string field = _listComboBoxObjects[i].SelectedItem.ToString();
                        string el = _listComboBoxXMLs[i].SelectedItem.ToString();
                        maps.Single(m => m.Field == field).AddElement(el);
                    }
                }
                MessageBox.Show("Mapping success!");

            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot Mapping!", "Error");
                return;
            }
            finally
            {
                this.Close();
            }
        }
    }
}
